<?php
    header('Content-type:text/html;charset=utf-8');
    // cookie
    $c = $_COOKIE["se"];
    $n = (int)base64_decode($c);
    if (!$c){
        setcookie("se", base64_encode((string)time()));
    }else{
        if (!$n){
            setcookie("se", base64_encode((string)time()));
            // die("{code:1}");
        }
        if (time() - $n < 10){
            die("{code:1}");
        }else{
            setcookie("se", base64_encode((string)time()));
        }
    }
    // run
    $inp = @$_REQUEST['p'] or die("{code:1}");
    // $inp = 'hello!!!';
    $finp = str_replace("\n", '[nl]', trim($inp));

    if(mb_strlen($finp) > 80 || mb_strlen($finp) === 0){
        die("{code:1}");
    }
    if(strstr($finp, '\\')){
        die("{code:1}");
    }

    $inp = "'".get_microtime()."':".str_replace(":","-",$_SERVER["REMOTE_ADDR"]).":'".$finp."'\n";
    $file_before = @file("./data/chat.dat") or die("{code:1}");
    $max_len = 50;
    array_push($file_before, $inp);
    if(count($file_before) > $max_len){
        array_splice($file_before, 0, 1);
    }
    $file_after = fopen("./data/chat.dat", "w");
    foreach($file_before as $v){
        fwrite($file_after, $v);
    }
    fclose($file_after);
    echo "{code:0}";
    function get_microtime(){
        list($msec, $sec) = explode(' ', microtime());
        $res = (string)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $res;
    }
?>